package com.qf.service;

import com.qf.constant.RabbitMQConstants;
import com.qf.constant.ReportStateConstants;
import com.qf.constant.SourceType;
import com.qf.model.StandardSubmit;
import com.qf.pojo.SmsInterfaceForm;
import com.qf.utils.IdWorker;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *  1、Java配置类   @Configuration  @Bean
 *
 *  2、RabbitAdmin  动态创建交换机与队列 并绑定
 *
 *  3、注解 创建交换机 注解创建队列  注解绑定   项目打算用这种办法
 *
 * @author lixu
 */
@Service
public class InterfaceServiceImpl implements InterfaceService {

    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private IdWorker idWorker;
    /**
     * 发送消息 到MQ
     * @param smsInterfaceForm
     * @return
     */
    @Override
    public boolean sendMessage(SmsInterfaceForm smsInterfaceForm) {

        //1:准备如下对象的数据
        StandardSubmit standardSubmit = new StandardSubmit();
        standardSubmit.setClientID(Integer.parseInt(smsInterfaceForm.getClientID()));
        standardSubmit.setSrcSequenceId(Long.parseLong(smsInterfaceForm.getSrcID()));
        standardSubmit.setMessageContent(smsInterfaceForm.getContent());
        standardSubmit.setReportState(ReportStateConstants.WAIT);
        standardSubmit.setSource(SourceType.HTTP);


        //手机号
        String mobile = smsInterfaceForm.getMobile();
        String[] ms = mobile.split(",");
        //根据手机号的多少发消息
        for (String m : ms) {
            //2:准备大量的数据   现在的时间-1970年
            long id = idWorker.nextId();//分布式ID  能排序
            standardSubmit.setSequenceId(String.valueOf(id));
            standardSubmit.setDestMobile(m);
            //3:发消息
            // 发送短信的消息队列名  ---  接口模块发送消息到策略模块
            //队列
            //public final static String TOPIC_PRE_SEND = "pre_send_sms_topic";
            //交换机名称
            //public final static String TOPIC_PRE_SEND = "pre_send_sms_topic_exchange";

            //创建交换机  创建队列  绑定交换机与队列
            rabbitTemplate.convertAndSend(
                    RabbitMQConstants.TOPIC_PRE_SEND + "_exchange",
                    "routingKey",
                    standardSubmit);
        }
        //成功
        return true;
    }
}
